import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
import * as menuService from '../../../services/menus';
import * as common from '../../../utils/common'
import * as compDiviService from '../../../services/AccountDivi';
const daoName = "BaseSjlrYxjsLs";
const ButtonGroup = Button.Group;
const columns = [
    { title: '工号', width: 100, key: 'CUID', dataIndex: "CUID",  sorter: true, dispaly: true, filter: true, export: false },
    { title: '姓名', width: 120, sorter: true, dataIndex: 'CUNM', key: 'CUNM', dispaly: true, filter: false, export: true },
    { title: '有效结算户数', width: 130, sorter: true, dataIndex: 'YXJSHS', key: 'YXJSHS', dispaly: true, filter: false, export: true },
    { title: '处理结果', width: 130, sorter: true, key: 'ISOK', dataIndex: 'ISOKCaption', dispaly: true, filter: false, export: true },
    { title: '失败原因', width: 180, sorter: true, key: 'CLJG', dataIndex: 'CLJG', dispaly: true, filter: false, export: true },
    { title: '年份', width: 130, sorter: true, key: 'NIAN', dataIndex: 'NIAN', dispaly: true, filter: false, export: true },
    { title: '季度', width: 130, sorter: true, key: 'JIDU', dataIndex: 'JIDU', dispaly: true, filter: false, export: true },
];
const controls = [
    { name: 'CUID', required: true, type: 'text', label: '工号', grid: 10 / 24 },
    { name: 'CUNM', required: true, type: 'text', label: '姓名', grid: 10 / 24 },
    { name: 'YXJSHS', required: false, type: 'number', dataType: "number", label: '有效结算户数',  grid: 10 / 24 },
    { name: 'ISOK', required: false, ignore: true, type: 'text', label: '处理结果', grid: 10 / 24 },
    { name: 'CLJG', required: true, ignore: true, type: 'text', label: '失败原因', grid: 10 / 24 },
    { name: 'NIAN', required: true, type: 'text', label: '年份', grid: 10 / 24 },
    { 
        name: 'JIDU', required: true, type: 'select', label: '季度', grid: 10 / 24,
        data :[{value:'1',label:"第一季度"},{value:'2',label:"第二季度"},{value:'3',label:"第三季度"},{value:'4',label:"第四季度"}]
    },
]
const columnsImp = [
    { title: '工号', key: 'CUID', width: 100, export: true },
    { title: '姓名', key: 'CUNM', width: 100, export: true },
    { title: '序列', key: 'XL', width: 100, export: true },
    { title: '职务', key: 'ZW', width: 100, export: true },
    { title: '部门', key: 'BM', width: 100, export: true },
    { title: '有效结算户数(每户)', key: 'YXJSHS', width: 200, export: true },
];
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
const DataImport = dataImportCreate(daoName);
class BaseSjlrYxjsLsForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataChange: false
        };
    }
    componentDidMount() {
        this.props.dispatch({
            type: `${daoName}/setKeyName`,
            payload: { keyName: "YXID", permID: this.props.permID }
        });
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 10 }
        });
        this.loadOptionData();
    }
    componentWillReceiveProps(nextProps) {
        const { showImport } = this.props
        if (!nextProps.showImport && showImport) {
            this.props.dispatch({
                type: `${daoName}/fetch`,
                payload: { daoName, page: 1, pageSize: 10 }
            });
        }
    }
    loadOptionData() {
        let options = []
        controls.map(c => {
            if (c.fetch) {
                let f = c.fetch;
                if (c.type === 'cascader-select') {
                    f.isCascader = true;
                }
                if (c.type !== 'search') {
                    options.push(f);
                }
            }
        })
        if (daoName && options.length > 0) {
            this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
        }
    }
    showImportHandler() {
        compDiviService.SelConfUpdt().then(function (data) {
            if (data==='false') {
                message.info("数据补录开关未开启")
            } else {
                this.props.dispatch({
                    type: `${daoName}_DataImport/setDisplay`,
                    payload: { display: true },
                });
            }
        }.bind(this))
    }
    DownTmportHandler() {
        let columnHeaders = []
        columnsImp.map((c, j) => {
            if (!c.export) {
                return
            }
            let header = {
                Name: c.key,
                Caption: c.title,
                Width: c.width
            }
            columnHeaders.push(header)
        })
        let form = document.createElement('form');//定义一个form表单
        let whereCauses = [];
        let sort = {
            field: 'BM',
            order: 'descend'
          }
        const orderItems = common.sorterToOrderItems(sort)
        form.style = "display:none";
        form.target = "_blank";
        form.method = "post";
        form.action = "/api/ExportXlsx/" + "VBaseKhjlSjlr";
        let input1 = document.createElement("input");
        input1.type = "hidden";
        input1.name = "WhereCauses";
        input1.value = JSON.stringify(whereCauses);
        let input2 = document.createElement("input");
        input2.type = "hidden";
        input2.name = "OrderItems";
        input2.value = JSON.stringify(orderItems)
        let input3 = document.createElement("input");
        input3.type = "hidden";
        input3.name = "ColumnHeaders";
        input3.value = JSON.stringify(columnHeaders);
        let input4 = document.createElement("input");
        input4.type = "hidden";
        input4.name = "PermID";
        input4.value = "" + this.props.permID;
        let input5 = document.createElement("input");
        input5.type = "hidden";
        input5.name = "UserID";
        input5.value = "" + this.props.userID;
        document.body.appendChild(form);//将表单放置在web中
        form.appendChild(input1);
        form.appendChild(input2);
        form.appendChild(input3);
        form.appendChild(input4);
        form.appendChild(input5);
        form.submit();
        document.body.removeChild(form)
    }
    render() {
        let { showEdit, record, showImport, permID, popedomTypes } = this.props
        const pTypes = popedomTypes[permID]
        let data = null
        if (pTypes.indexOf("2") !== -1) {
            data = <ButtonGroup style={{ zIndex: 0 }}>
                <Button onClick={this.showImportHandler.bind(this)}>导入</Button>
                <Button onClick={this.DownTmportHandler.bind(this)}>下载模板</Button>
            </ButtonGroup>
        }
        return (
            <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
                <div>
                    <div className={styles.create}>
                        {data}
                    </div>
                    <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
                    <DataImport title="对公有效户" importName="BaseSjlrYxjsLs" daoName={daoName} keys={["YXID"]} paramNames={["NIAN","JIDU"]} importMode="TruncteAfterInsert" controls={controls} />
                </div>
            </div>
        )
    }
}

function mapStateToProps(state) {
    const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
    const { userID, orgID, pOrgID, popedomTypes } = state.menus;
    const { display } = state[`${daoName}_DataImport`];
    return {
        userID,
        orgID,
        pOrgID,
        popedomTypes,
        showEdit,
        selectedRowKeys,
        keyName,
        record,
        showImport: display
    }
}
BaseSjlrYxjsLsForm = connect(mapStateToProps)(BaseSjlrYxjsLsForm)
export default BaseSjlrYxjsLsForm;

let render = (props) => React.createElement(BaseSjlrYxjsLsForm, props);
let f = new Factory()
f.set("BaseSjlrYxjsLsForm", render)
